package cn.itsource.pet.service.impl;

import cn.itsource.basic.domain.Point;
import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.org.domain.Shop;
import cn.itsource.org.mapper.ShopMapper;
import cn.itsource.pet.domain.SearchMasterMsg;
import cn.itsource.pet.service.ISearchMasterMsgService;
import cn.itsource.user.domain.User;
import cn.itsource.utils.DistanceUtil;
import cn.itsource.utils.LoginContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {

    @Autowired
    private ShopMapper shopMapper;

    @Override
    public void publish(SearchMasterMsg searchMasterMsg,HttpServletRequest request) {
        //获取当前的登录用户
        User user= (User) LoginContext.getCurrent(request);
        //寻主消息关联当前用户
        searchMasterMsg.setUser(user);
        //获取离用户最近的门店
        //1.把用户输入的地址转换为经纬度
        Point point = DistanceUtil.getPoint(searchMasterMsg.getAddress());
        //2.获取最近门店
        List<Shop> shops = shopMapper.loadAll();
        Shop shop = DistanceUtil.getNearestShop(point, shops);
        searchMasterMsg.setShop(shop);
        //处理状态方面的问题  待处理
        searchMasterMsg.setState(0);


        super.save(searchMasterMsg);

    }
}
